home *** CD-ROM | disk | FTP | other *** search
- { rubber.pas -- Click and drag rubberband outlines }
-
- program Rubber;
-
- {$R rubber.res}
-
- uses WinTypes, WinProcs, WObjects;
-
- const
-
- id_Menu = 100; { Menu resource ID }
- cm_Lines = 101; { Menu command values }
- cm_Rectangles = 102;
- cm_Circles = 103;
- cm_Clear = 104;
- cm_Quit = 105;
-
- type
-
- RubberApplication = object(TApplication)
- procedure InitMainWindow; virtual;
- end;
-
- PRubberWindow = ^RubberWindow;
- RubberWindow = object(TWindow)
- DC: Hdc;
- ButtonDown: Boolean;
- X1, Y1, X2, Y2: Integer;
- CurrentObject: Integer;
- OldBrush: HBrush;
- constructor Init(AParent: PWindowsObject; ATitle: PChar);
- procedure ShowMouseLocation(X, Y: Integer);
- procedure DrawRubberband;
- procedure WMCommand(var Msg: TMessage);
- virtual wm_First + wm_Command;
- procedure WMLButtonDown(var Msg: TMessage);
- virtual wm_First + wm_LButtonDown;
- procedure WMLButtonUp(var Msg: TMessage);
- virtual wm_First + wm_LButtonUp;
- procedure WMMouseMove(var Msg: TMessage);
- virtual wm_First + wm_MouseMove;
- end;
-
-
- {- Toggle a checkmarked menu item on or off }
- procedure ToggleCheck(Menu: HMenu; MenuItemID: Word);
- var
- MAttr, WCheck: Word;
- begin
- MAttr := GetMenuState(Menu, MenuItemID, mf_ByCommand);
- if (MAttr and mf_Checked) = mf_Checked then
- WCheck := mf_ByCommand or mf_Unchecked
- else
- WCheck := mf_ByCommand or mf_Checked;
- CheckMenuItem(Menu, MenuItemID, WCheck)
- end;
-
-
- { RubberApplication }
-
- {- Initialize RubberApplication object's window }
- procedure RubberApplication.InitMainWindow;
- begin
- MainWindow := New(PRubberWindow, Init(nil, 'Rubber Outlines'))
- end;
-
-
- { RubberWindow }
-
- {- Construct RubberWindow object }
- constructor RubberWindow.Init(AParent: PWindowsObject; ATitle: PChar);
- begin
- TWindow.Init(AParent, ATitle);
- Attr.Menu := LoadMenu(HInstance, PChar(id_Menu));
- ButtonDown := false;
- CurrentObject := cm_Lines;
- ToggleCheck(Attr.Menu, CurrentObject)
- end;
-
- {- Display the mouse's "hot spot" location }
- procedure RubberWindow.ShowMouseLocation(X, Y: Integer);
- var
- SX, SY: String[6]; { Room for Integers in string form }
- S: String[24]; { Both coordinates in string form }
- begin
- Str(X:5, SX);
- Str(Y:5, SY);
- S := SX + ' :' + SY + ' ';
- TextOut(DC, 10, 10, @S[1], Length(S))
- end;
-
- {- Draw rubberband outline while dragging mouse }
- procedure RubberWindow.DrawRubberband;
- begin
- MoveTo(DC, X1, Y1);
- case CurrentObject of
- cm_Lines: LineTo(DC, X2, Y2);
- cm_Rectangles: Rectangle(DC, X1, Y1, X2, Y2);
- cm_Circles: Ellipse(DC, X1, Y1, X2, Y2);
- end
- end;
-
- {- Respond to left mouse button down events }
- procedure RubberWindow.WMLButtonDown(var Msg: TMessage);
- begin
- if not ButtonDown then with Msg do
- begin
- DC := GetDC(HWindow);
- ShowMouseLocation(LOWORD(LParam), HIWORD(LParam)); {i.e. X, Y}
- X1 := LParamLo;
- Y1 := LParamHi;
- X2 := X1;
- Y2 := Y1;
- OldBrush:= SelectObject(DC, GetStockObject(hollow_Brush));
- SetROP2(DC, r2_Not);
- DrawRubberband;
- ButtonDown := true;
- SetCursor(LoadCursor(0, idc_Cross));
- SetCapture(HWindow)
- end
- end;
-
- {- Respond to left mouse button up events }
- procedure RubberWindow.WMLButtonUp(var Msg: TMessage);
- begin
- if ButtonDown then with Msg do
- begin
- DrawRubberband;
- ButtonDown := false;
- SetROP2(DC, r2_Black);
- DrawRubberband; { Draw visible object }
- SelectObject(DC, OldBrush);
- SetCursor(LoadCursor(0, idc_Arrow));
- ReleaseDC(HWindow, DC);
- ReleaseCapture
- end
- end;
-
- {- Respond to mouse-movement event }
- procedure RubberWindow.WMMouseMove(var Msg: TMessage);
- begin
- if ButtonDown then with Msg do
- begin
- ShowMouseLocation(LOWORD(LParam), HIWORD(LParam)); {i.e. X, Y}
- DrawRubberband; { Erase old line }
- with Msg do
- begin
- X2 := LParamLo;
- Y2 := LParamHi;
- DrawRubberband { Draw new line }
- end
- end
- end;
-
- {- Intercept wm_Command messages }
- procedure RubberWindow.WMCommand(var Msg: TMessage);
- begin
- with Msg do
- case WParam of
- cm_Lines, cm_Rectangles, cm_Circles:
- begin
- ToggleCheck(Attr.Menu, CurrentObject);
- CurrentObject := WParam;
- ToggleCheck(Attr.Menu, CurrentObject)
- end;
- cm_Clear: InvalidateRect(HWindow, nil, true);
- cm_Quit: CloseWindow
- else
- TWindow.WMCommand(Msg)
- end
- end;
-
- var
-
- RubberApp: RubberApplication;
-
- begin
- RubberApp.Init('RubberApp');
- RubberApp.Run;
- RubberApp.Done
- end.
-
-
- {--------------------------------------------------------------
- Copyright (c) 1991 by Tom Swan. All rights reserved.
- Revision 1.00 Date: 3/09/1991
- ---------------------------------------------------------------}
-